package com.tencreat.mall.controller.admin;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tencreat.common.annotation.Log;
import com.tencreat.common.core.HttpResult;
import com.tencreat.common.core.controller.BaseController;
import com.tencreat.common.core.domain.AjaxResult;
import com.tencreat.common.core.domain.model.ReqIdParam;
import com.tencreat.common.core.domain.model.ReqIdsParam;
import com.tencreat.common.enums.BusinessType;
import com.tencreat.common.utils.StringUtils;
import com.tencreat.common.utils.poi.ExcelUtil;
import com.tencreat.domain.mall.MallConsult;
import com.tencreat.domain.mall.dto.MallConsultRequDTO;
import com.tencreat.service.mall.IMallConsultService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 资讯管理Controller
 *
 * @author tencreat
 * @date 2022-01-10
 */
@Api(tags="资讯管理api")
@RestController
@RequestMapping("/system/consult")
public class AdminMallConsultController extends BaseController {
    @Autowired
    private IMallConsultService mallConsultService;

    /**
     * 查询资讯管理列表
     */
    @ApiOperation(value="查询资讯管理列表")
    @PreAuthorize("@ss.hasPermi('system:consult:list')")
    @PostMapping("/list")
    public HttpResult<IPage<MallConsult>> list(@RequestBody MallConsultRequDTO mallConsultRequVo) {
        MallConsult mallConsult = new MallConsult();
        BeanUtils.copyProperties(mallConsultRequVo,mallConsult);
        mallConsult.setTitle(null);
        QueryWrapper<MallConsult> wrapper = Wrappers.query(mallConsult);
        if(mallConsultRequVo.getStartTime() != null){
            wrapper.gt("create_time", mallConsultRequVo.getStartTime().toLocalDate());
            wrapper.lt("create_time", mallConsultRequVo.getEndTime().toLocalDate());
        }
        if(StringUtils.isNotEmpty(mallConsultRequVo.getTitle())){
            wrapper.like("title", mallConsultRequVo.getTitle());
        }
        if (mallConsultRequVo.getSortStr() != null && mallConsultRequVo.getSortDesc() != null){
            if (mallConsultRequVo.getSortDesc() == "ascending"){
               wrapper.orderByAsc(mallConsultRequVo.getSortStr());
            }
            wrapper.orderByDesc(mallConsultRequVo.getSortStr());
        }else {
            wrapper.orderByDesc("create_time");
        }
        Page<MallConsult> page =  new Page<>(mallConsult.getPageNum(), mallConsult.getPageSize());
        return HttpResult.ok(mallConsultService.page(page, wrapper));
    }

    /**
     * 导出资讯管理列表
     */
    @ApiOperation(value="导出资讯管理列表")
    @PreAuthorize("@ss.hasPermi('system:consult:export')")
    @Log(title = "商城管理>资讯管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public AjaxResult export(MallConsult mallConsult) {
        List<MallConsult> list = mallConsultService.selectMallConsultList(mallConsult);
        ExcelUtil<MallConsult> util = new ExcelUtil<MallConsult>(MallConsult.class);
        return util.exportExcel(list, "consult");
    }

    /**
     * 获取资讯管理详细信息
     */
    @ApiOperation(value="获取资讯管理详细信息")
    @PreAuthorize("@ss.hasPermi('system:consult:query')")
    @PostMapping(value = "/getInfo")
    public HttpResult<MallConsult> getInfo(@RequestBody   ReqIdParam id) {
        return HttpResult.ok(mallConsultService.selectMallConsultById(id));
    }

    /**
     * 新增资讯管理
     */
    @ApiOperation(value="新增资讯管理")
    @PreAuthorize("@ss.hasPermi('system:consult:add')")
    @Log(title = "商城管理>资讯管理", businessType = BusinessType.INSERT)
    @PostMapping(value = "/add")
    public HttpResult<Boolean> add(@RequestBody  MallConsult mallConsult) {

        return to(mallConsultService.insertMallConsult(mallConsult));
    }

    /**
     * 修改资讯管理
     */
    @ApiOperation(value="修改资讯管理")
    @PreAuthorize("@ss.hasPermi('system:consult:edit')")
    @Log(title = "商城管理>资讯管理", businessType = BusinessType.UPDATE)
    @PostMapping(value = "/edit")
    public HttpResult<Boolean> edit(@RequestBody  MallConsult mallConsult) {
        return to(mallConsultService.updateMallConsult(mallConsult));
    }

    /**
     * 删除资讯管理
     */
    @ApiOperation(value="删除资讯管理")
    @PreAuthorize("@ss.hasPermi('system:consult:remove')")
    @Log(title = "商城管理->资讯管理", businessType = BusinessType.DELETE)
	@PostMapping("/remove")
    public HttpResult<Boolean> remove(@RequestBody  ReqIdsParam ids) {
        return to(mallConsultService.deleteMallConsultByIds(ids));
    }
}
